package com.xsq.wifi.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.xsq.wifi.R;
import com.xsq.wifi.databinding.ActivityMainBinding;
import com.xsq.wifi.fragment.DeviceFragment;
import com.xsq.wifi.fragment.MineFragment;
import com.xsq.wifi.fragment.SourceFragment;
import com.xsq.wifi.utils.SSDPScanner;

import java.util.ArrayList;
import java.util.List;

/**
 * 编写人：李振德
 * 创建时间：2025/05/07
 */

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";
    private ActivityMainBinding binding;

    private List<Fragment> mBaseFragment;
    private int position;
    private Fragment mContent;

    public String[] permissions = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mBaseFragment = new ArrayList<>();
        mBaseFragment.add(new DeviceFragment());
        mBaseFragment.add(new SourceFragment());
        mBaseFragment.add(new MineFragment());
        checked(0);
        binding.llDevice.setOnClickListener(view -> {
            checked(0);
        });
        binding.llSource.setOnClickListener(view -> {
            checked(1);
        });
        binding.llMine.setOnClickListener(view -> {
            checked(2);
        });
        XXPermissions.with(MainActivity.this)
                // 申请多个权限
                .permission(permissions)
                // 设置权限请求拦截器（局部设置）
                //.interceptor(new PermissionInterceptor())
                // 设置不触发错误检测机制（局部设置）
                //.unchecked()
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                        } else {
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                        } else {
                        }
                    }
                });
    }
    public void checked(int p) {
        this.position = p;
        setSelected(p);
        switchFragment(this.mContent, getFragment());
        if(p==0){
            binding.deviceText.setTextColor(getResources().getColor(R.color.maincolor));
            binding.sourceText.setTextColor(getResources().getColor(R.color.color_666666));
            binding.mineText.setTextColor(getResources().getColor(R.color.color_666666));
        }else if(p==1){
            binding.deviceText.setTextColor(getResources().getColor(R.color.color_666666));
            binding.sourceText.setTextColor(getResources().getColor(R.color.maincolor));
            binding.mineText.setTextColor(getResources().getColor(R.color.color_666666));
        }else if(p==2){
            binding.deviceText.setTextColor(getResources().getColor(R.color.color_666666));
            binding.sourceText.setTextColor(getResources().getColor(R.color.color_666666));
            binding.mineText.setTextColor(getResources().getColor(R.color.maincolor));
        }

    }
    public void setSelected(int position) {
        binding.llDevice.setSelected(position == 0);
        binding.llSource.setSelected(position == 1);
        binding.llMine.setSelected(position == 2);
    }
    private Fragment getFragment() {
        return this.mBaseFragment.get(this.position);
    }
    private void switchFragment(Fragment from, Fragment to) {
        if (from != to) {
            this.mContent = to;
            FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
            if (to.isAdded()) {
                if (from != null) {
                    beginTransaction.hide(from);
                }
                if (to != null) {
                    beginTransaction.show(to).commit();
                    return;
                }
                return;
            }
            if (from != null) {
                beginTransaction.hide(from);
            }
            if (to != null) {
                beginTransaction.add(binding.fragmentContent.getId(), to).commit();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onStop() {
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
